package com.ywq.app.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ywq.app.biz.ArtFindBiz;
import com.ywq.app.biz.IdentifierBiz;
import com.ywq.app.dao.ArtistDao;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtArtistPic;
import com.ywq.app.domain.ArtMap;
import com.ywq.app.domain.FindSo;
import com.ywq.app.domain.PicComment;
import com.ywq.app.domain.PwSchool;
import com.ywq.common.constant.Constant;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
public class ArtFindBizImpl  implements ArtFindBiz{
	
	private final static Logger logger = LoggerFactory.getLogger(ArtFindBizImpl.class);
	@Autowired
	private IdentifierBiz idBiz;
	private ArtistDao artistDao;
	public void setArtistDao(ArtistDao artistDao) {
		this.artistDao = artistDao;
	}
	//艺术家的作品列表
	@Override
	public PageModel getArtistPicList(Integer page,String artistId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		map.put("artistId",artistId);
		int count = artistDao.getArtistPicListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getArtistPicList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1)/ PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}
	
	@Override
	public PageModel getMadeArtistList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		map.put("type",PageSize.ARTIST_MADE);
		int count = artistDao.getArtistListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getMadeArtistList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	/*@Override
	public PageModel getMadeArtistCsList(Integer page,String classId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		map.put("type",PageSize.ARTIST_MADE);
		map.put("artClass",classId);
		int count = artistDao.getArtistListCsCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getMadeArtistCsList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}*/
	//展示页面艺术家列表页
	@Override
	public PageModel getMadeNiceArtistList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE_NICE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE_NICE);
		map.put("type",PageSize.ARTIST_MADE);
		int count = artistDao.getArtistListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getMadeArtistList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_MADE_NICE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE_NICE - 1)/ PageSize.PAGE_SIZE_MADE_NICE);
		return pm;
	}
	//查询作品根据艺术家标识
	@Override
	public List<ArtArtistPic> getPicListByIstId(String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("artistId",id);
		List<ArtArtistPic> picList =artistDao.getArtistPicList(map);
		return picList;
	}
	//发现的艺术家
		@Override
		public PageModel getArtistListForMisc(Integer page,Integer pageSize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("type",PageSize.ARTIST_FIND);
			int count = artistDao.getArtistListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtistList(map));
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize - 1)/ pageSize);
			return pm;
		}
		
		@Override
		public PageModel getArtistPicListForMisc(Integer page,String artistId,Integer pageSize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("artistId",artistId);
			int count = artistDao.getArtistPicListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtistPicList(map));
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize - 1)/ pageSize);
			return pm;
		}
		@Override
		public List<PwSchool> getCompanyByName(String name) throws AppBusinessException {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("name", name);
			return artistDao.getCompanyByName(map);
		}
		@Override
		public PwSchool getSchool(String id) throws AppBusinessException {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("schoolid", id);
			return artistDao.getSchool(map);
		}
		//添加艺术家
		@Override
		public void insertArtArtist(ArtArtist artArtist) throws AppBusinessException {
				String istId = idBiz.generateId("art_artist");
				artArtist.setId(istId);
				artArtist.setType(PageSize.ARTIST_FIND);
				artistDao.insertArtArtist(artArtist);
			}
		//查询发现的作品
		@Override
		public PageModel getFindPicList(Integer page,FindSo findSo) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * PageSize.PAGE_SIZE_FIND);
			map.put("pageSize", PageSize.PAGE_SIZE_FIND);
			map.put("type",PageSize.ARTIST_FIND);
			//
			//map.put("fine",findSo.getFine());
			//
			if(findSo.getChoice()){
				map.put("choice",findSo.getChoice());
			}
			//
			if(findSo.getBestLove()){
				map.put("bestLove","true");
			}
			if(findSo.getBestMore()){
				map.put("bestMore","true");
			}
			if(findSo.getBestNew()){
				map.put("bestNew","true");
			}
			if(findSo.getMonth()){
				Calendar cal = Calendar.getInstance();
				 int month = cal.get(Calendar.MONTH) + 1;
				int year = cal.get(Calendar.YEAR);
				map.put("month",month);
				map.put("year",year);
			}
			if(findSo.getWeek()){
				SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Calendar currentDate = Calendar.getInstance();
				int weekday=currentDate.get(7)-1;
				currentDate.add(5, -weekday);
				Date start=currentDate.getTime();
				String st =f.format(start);
				currentDate.add(5, 6);
				Date end =currentDate.getTime();
				String ed =f.format(end);
				map.put("end", ed);
				map.put("start", st);
			}else{
				map.put("end",findSo.getEndDate());
				map.put("start",findSo.getStartDate());
			}
			//
			map.put("artsize",findSo.getSize());
			map.put("artrange",findSo.getPrice());
			map.put("artfind",findSo.getFind());
			map.put("artground",findSo.getGround());
			map.put("artuse",findSo.getUse());
			map.put("arttag",findSo.getTag());
			map.put("artmaterial",findSo.getMaterial());
			//
			int count = artistDao.getFindPicListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getFindPicList(map));
			pm.setPageSize(PageSize.PAGE_SIZE_FIND);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + PageSize.PAGE_SIZE_FIND - 1)/ PageSize.PAGE_SIZE_FIND);
			return pm;
		}
		//发现艺术家
		@Override
		public PageModel getFindArtistList(Integer page,String province,String school) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * PageSize.PAGE_SIZE_FIND_ARTIST);
			map.put("pageSize", PageSize.PAGE_SIZE_FIND_ARTIST);
			map.put("type",PageSize.ARTIST_FIND);
			map.put("provinceCode",province);
			map.put("schoolId",school);
			int count = artistDao.getArtistListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			List<ArtArtist> list=artistDao.getMadeArtistList(map);
			for(int i= 0;i<list.size();i++){
				ArtArtist artist=list.get(i);
				if(artist.getSchoolid()!=null && !"".equals(artist.getSchoolid())){
					PwSchool pwSchool =getSchool(artist.getSchoolid());
					artist.setSchoolName(pwSchool.getName());
				}
				ArtArtist allCount=getAllCountByArtistId(artist.getId());
				if(allCount!=null){
					artist.setViewAmount(allCount.getViewAmount());
					artist.setLoveAmount(allCount.getLoveAmount());
				}
			}
			pm.setList(list);
			pm.setPageSize(PageSize.PAGE_SIZE_FIND_ARTIST);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + PageSize.PAGE_SIZE_FIND_ARTIST - 1)/ PageSize.PAGE_SIZE_FIND_ARTIST);
			return pm;
		}
		//根据城市查询艺术家数量
		@Override
		public  List<ArtMap> getNumByCtiy()throws AppBusinessException {
			return artistDao.getNumByCtiy();
		}
		//评论
		@Override
		public List<PicComment> getFindCommentByPicId(String picId)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageSize", PageSize.PAGE_SIZE_COMMENT);
			map.put("startId", 0);
			map.put("picId", picId);
			return artistDao.getFindCommentByPicId(map);
		}
		 //评论列表
		@Override
		public PageModel getFindCommentModelByPicId(Integer page,String picId) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * PageSize.PAGE_SIZE_COMMENTMAX);
			map.put("pageSize", PageSize.PAGE_SIZE_COMMENTMAX);
			map.put("picId",picId);
			int count = artistDao.getFindCommentCountByPicId(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getFindCommentByPicId(map));
			pm.setPageSize(PageSize.PAGE_SIZE_COMMENTMAX);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + PageSize.PAGE_SIZE_COMMENTMAX - 1)/ PageSize.PAGE_SIZE_COMMENTMAX);
			return pm;
		}
		//评论数
		@Override
		public int getFindCommentCountByPicId(String picId)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("picId", picId);
			return artistDao.getFindCommentCountByPicId(map);
		}
		//新增评论
		@Override
		public void insertComment(PicComment picComment) throws AppBusinessException {
				String istId = idBiz.generateId("pic_comment");
				picComment.setId(istId);
				artistDao.insertComment(picComment);
			}
		//一级分类页面
		@Override
		public PageModel getPrintPicList(Integer page,FindSo findSo) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * PageSize.PAGE_SIZE_FIND);
			map.put("pageSize", PageSize.PAGE_SIZE_FIND);
			map.put("type",PageSize.ARTIST_FIND);
			map.put("artsize",findSo.getSize());
			map.put("artrange",findSo.getPrice());
			map.put("artHot",findSo.getHot());
			map.put("artground",findSo.getGround());
			map.put("artuse",findSo.getUse());
			map.put("artProf",findSo.getProf());
			//
			int count = artistDao.getPrintPicListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getPrintPicList(map));
			pm.setPageSize(PageSize.PAGE_SIZE_FIND);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + PageSize.PAGE_SIZE_FIND - 1)/ PageSize.PAGE_SIZE_FIND);
			return pm;
		}
		//二级分类
		@Override
		public PageModel getPrintsPicList(Integer page,FindSo findSo) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * PageSize.PAGE_SIZE_FIND);
			map.put("pageSize", PageSize.PAGE_SIZE_FIND);
			map.put("type",PageSize.ARTIST_FIND);
			map.put("artsize",findSo.getSize());
			map.put("artrange",findSo.getPrice());
			map.put("artHot",findSo.getHot());
			map.put("artProf",findSo.getProf());
			map.put("artground",findSo.getGround());
			map.put("artuse",findSo.getUse());
			//
			int count = artistDao.getFindPicListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getFindPicList(map));
			pm.setPageSize(PageSize.PAGE_SIZE_FIND);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + PageSize.PAGE_SIZE_FIND - 1)/ PageSize.PAGE_SIZE_FIND);
			return pm;
		}
		 //评论列表会员
		@Override
		public PageModel getFindCommentModelByMemberId(Integer page,String memberId) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * PageSize.PAGE_SIZE_COMMENTMAX);
			map.put("pageSize", PageSize.PAGE_SIZE_COMMENTMAX);
			map.put("memberId",memberId);
			int count = artistDao.getFindCommentCountByPicId(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getFindCommentByPicId(map));
			pm.setPageSize(PageSize.PAGE_SIZE_COMMENTMAX);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + PageSize.PAGE_SIZE_COMMENTMAX - 1)/ PageSize.PAGE_SIZE_COMMENTMAX);
			return pm;
		}
		//
		@Override
		public ArtArtist getAllCountByArtistId(String artistId)throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("artistId", artistId);
			return artistDao.getAllCountByArtistId(map);
		}
		
	}
